package com.managers
{	
	import com.states.State;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.utils.Timer;
	/**
	 * @project FindTheEarl
	 * @file asfdassad.as
	 * @author Matthew Ladouceur
	 * @created Aug 12, 2011
	 *
	 * Copyright (c) 2011 marblemedia Interactive Inc.
	 * All Rights Reserved.
	 *
	 * http://www.marblemedia.com
	 */
	public class StateManager extends Sprite
	{
		private var _currentState:State;
		private var _prevState:Vector.<State> = new Vector.<State>;
		
		public function get currentState():State {
			return _currentState;
		}
		
		public function StateManager() 
		{
			
		}
		
		public function update():void {
			if(_currentState !=null){
				_currentState.update();
				_currentState.draw();
			}
		}
		
		public function changeState(state:State):void {
			if (_currentState != null){
				while (_prevState.length > 0){
					_currentState.kill();
					_currentState = _prevState.pop();
				}
				_currentState.kill();
			}
			_currentState = state;
			_currentState.init();
			addChild(_currentState);
		}
		public function pushState(state:State):void {//needs updates
			if (_currentState != null) {
				_prevState.push(_currentState);
				_currentState.silence();
			}
			_currentState = state;
			_currentState.init();
			addChild(_currentState);
		}
		
		public function previousState():void {
			if (_prevState.length > 0) {
				_currentState.kill();
				_currentState = _prevState.pop();
				//if(_currentState.sleeping)
				//	_currentState.wakeUp();
				//else
					_currentState.init();
					addChild(_currentState);
				//Helper.Instance.stage.focus = Helper.Instance.stage;
			}
		}
		
		public function kill():void {
			_currentState.kill();
		}
		
		private static var _instance:StateManager;
		
		public static function get instance():StateManager {
			if (_instance == null) {
				_instance = new StateManager();
			}
			return _instance;
		}
	}

}